package com.mowanka.mokeng.ui.fragment.store

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.kingja.loadsir.core.LoadService
import com.mowanka.mokeng.app.base.BaseFragment
import com.mowanka.mokeng.app.ext.*
import com.mowanka.mokeng.databinding.FragmentStoreHomeBinding
import com.mowanka.mokeng.viewmodel.request.RequestStoreViewModel
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.parseState

class StoreHomeFragment : BaseFragment<BaseViewModel, FragmentStoreHomeBinding>() {

    //界面状态管理者
    private lateinit var loadsir: LoadService<Any>

    private var title = arrayListOf<String>()

    private var fragments = arrayListOf<Fragment>()

    private val requestStoreViewModel by viewModels<RequestStoreViewModel>()

    override fun initView(savedInstanceState: Bundle?) {
        //状态页配置
        loadsir = loadServiceInit(mDatabind.viewPager) {
            loadsir.showLoading()
            requestStoreViewModel.floor()
        }
    }

    override fun lazyLoadData() {
        loadsir.showLoading()
        requestStoreViewModel.floor()
    }

    override fun createObserver() {
        requestStoreViewModel.floorResponse.observe(this) { resultState ->
            parseState(resultState, {
                title.clear()
                fragments.clear()
                it.forEachIndexed { index, floorResponse ->
                    title.add(it[0].name)
                    fragments.add(StoreVariableFragment(it[0]))

//                    title.add(floorResponse.name)
//                    fragments.add(
//                        StoreVariableFragment(floorResponse)
//                    )
                }
                mDatabind.viewPager.init(this, fragments).apply { offscreenPageLimit = fragments.size }
                mDatabind.magicIndicator.bindViewPager2(mDatabind.viewPager, title)
                loadsir.showSuccess()
            }, {
                loadsir.showError(it.errorMsg)
            })
        }
    }
}